package cn.edu.tsinghua.thss.alg.closestpair;

import java.util.HashSet;
import java.util.Random;

public class Rand {

	/**
	 * Generate numerous points randomly.
	 * 
	 * @param size
	 *            the number of points to generate
	 * @return points
	 */
	public static Point[] getRandomPoints(int size) {
		Point[] points = new Point[size];
		Random random = new Random(200);
		for (int i = 0; i < size; i++) {
			double x = random.nextDouble() * 100;
			double y = random.nextDouble() * 100;
			points[i] = new Point(x, y);
		}

		return points;
	}

	/**
	 * Generate numerous points randomly and ensure there's no repeated points
	 * 
	 * @param size
	 *            the number of points to generate
	 * @return points
	 */
	public static Point[] getRandomPointsNoRepeat(int size) {
		Point[] points = new Point[size];
		HashSet<Point> pointset = new HashSet<Point>();
		Random random = new Random(200);
		for (int i = 0; i < size; i++) {
			double x = random.nextDouble() * 100;
			double y = random.nextDouble() * 100;
			points[i] = new Point(x, y);
			if (pointset.contains(points[i]))
				points[i--] = null;
			else
				pointset.add(points[i]);
		}
		System.out.println(pointset.size());

		return points;
	}

}
